﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace LearningBubbles
{

    abstract class Element
    {

        /// <summary>
        /// Is the element grabbed?
        /// </summary>
        private bool isGrabbed = false;

        public bool IsGrabbed
        {
            get { return isGrabbed; }
            set { isGrabbed = value; }
        }

        /// <summary>
        /// Who grabbed this element?
        /// (playerNumber)
        /// </summary>
        private int grabbedBy = 5;

        public int GrabbedBy
        {
            get { return grabbedBy; }
            set { grabbedBy = value; }
        }

        /// <summary>
        /// The current position of the element
        /// </summary>
        protected Vector2 position = new Vector2(Game1.random.Next()%640, 0);

        /// <summary>
        /// The previous speed of an element,
        /// if the speed changes
        /// </summary>
        private float previousSpeed;

        public float PreviousSpeed
        {
            get { return previousSpeed; }
            set { previousSpeed = value; }
        }

        /// <summary>
        /// The position of the appearance of
        /// the element in the assignment text
        /// </summary>
        private int posInText;

        public int PosInText
        {
            get { return posInText; }
            set { posInText = value; }
        }

        /// <summary>
        /// The value of the element
        /// </summary>
        private Object value;

        /// <summary>
        /// The speed of the element
        /// </summary>
        private float speed = 1.5f;

        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        /// <summary>
        /// The angle of the element
        /// </summary>
        protected float angle = 45 + Game1.random.Next(135);

        public Object Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        
        /// <summary>
        /// Move the element at the current position
        /// with the current speed and angle
        /// </summary>
        /// <param name="gameTime"></param>
        public void Move(GameTime gameTime)
        {
            position.X += (Speed * (float)Math.Cos(ToRadian(angle))) * 1 + (gameTime.ElapsedGameTime.Milliseconds) * 0.001f;
            position.Y += (Speed * (float)Math.Sin(ToRadian(angle))) * 1 + (gameTime.ElapsedGameTime.Milliseconds) * 0.001f;
            OnEdge();
        }

        /// <summary>
        /// Get the radian value of the angle
        /// </summary>
        /// <param name="ro"></param>
        /// <returns>angle in radians</returns>
        public static float ToRadian(float ro)
        {
            float radian = (float)((ro / 180) * Math.PI);
            return radian;
        }

        /// <summary>
        /// Make the element appear on
        /// the other side of the window
        /// if going too wide as well as
        /// on top again if falling down
        /// </summary>
        public void OnEdge()
        {
            if (position.X > 640) position.X = 0;
            else if (position.X < 0) position.X = 640;
            else if (position.Y > 480) position.Y = 0;
        }
    }
}
